{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读出目录的所有文件"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个会读出完整路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe']\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def get_files_by_extensions(extensions):\n",
    "    current_directory = os.getcwd()\n",
    "    files = []\n",
    "    for file_name in os.listdir(current_directory):\n",
    "        file_path = os.path.join(current_directory, file_name)\n",
    "        if os.path.isfile(file_path) and any(file_name.endswith(ext) for ext in extensions):\n",
    "            files.append(file_path)\n",
    "    return files\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 让用户输入文件类型，可以输入多个类型，以空格分隔，例如：cpp h txt\n",
    "    extensions = [\"exe\"]\n",
    "    files = get_files_by_extensions(extensions)\n",
    "    print(files)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这个读出文件名"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 269,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ceshi.cpp', 'ceshi.h', 'inputparameters.cpp', 'inputparameters_2.cpp', 'main.cpp', 'mainwindow.cpp', 'mainwindow.h', 'mlog.cpp', 'mythread.cpp', 'paramcal.cpp', 'paramcal_2.cpp', 'queryparameters.cpp']\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def get_files_by_extensions(extensions):\n",
    "    current_directory = os.getcwd()\n",
    "    files = []\n",
    "    for file_name in os.listdir(current_directory):\n",
    "        if os.path.isfile(file_name) and any(file_name.endswith(ext) for ext in extensions):\n",
    "            files.append(file_name)\n",
    "    return files\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 让用户输入文件类型，可以输入多个类型，以空格分隔，例如：cpp h txt\n",
    "    extensions = ['cpp', 'h']\n",
    "\n",
    "    files = get_files_by_extensions(extensions)\n",
    "    print(files)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "格式化代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 270,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: clang in c:\\users\\lenovo\\.conda\\envs\\pytorch\\lib\\site-packages (16.0.1.1)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip available: 22.3.1 -> 23.2.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: clang-tools in c:\\users\\lenovo\\.conda\\envs\\pytorch\\lib\\site-packages (0.8.0)\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "[notice] A new release of pip available: 22.3.1 -> 23.2.1\n",
      "[notice] To update, run: python.exe -m pip install --upgrade pip\n"
     ]
    }
   ],
   "source": [
    "! pip install clang\n",
    "! pip install clang-tools"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 271,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c:\\Users\\Lenovo\\Desktop\\Softness\\my\\code\\clang-format.exe\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "def get_files_by_extensions(extensions):\n",
    "    current_directory = os.getcwd()\n",
    "    files = []\n",
    "    for file_name in os.listdir(current_directory):\n",
    "        file_path = os.path.join(current_directory, file_name)\n",
    "        if os.path.isfile(file_path) and any(file_name.endswith(ext) for ext in extensions):\n",
    "            files.append(file_path)\n",
    "    return files\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 让用户输入文件类型，可以输入多个类型，以空格分隔，例如：cpp h txt\n",
    "    extensions = [\"exe\"]\n",
    "    clang_format_path = get_files_by_extensions(extensions)\n",
    "    clang_format_path = \"\\n\".join(clang_format_path)\n",
    "    print(clang_format_path)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 272,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceshi.cpp\n",
      "Error occurred while formatting ceshi.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'ceshi.cpp']' returned non-zero exit status 1.\n",
      "ceshi.h\n",
      "Error occurred while formatting ceshi.h: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'ceshi.h']' returned non-zero exit status 1.\n",
      "inputparameters.cpp\n",
      "Error occurred while formatting inputparameters.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'inputparameters.cpp']' returned non-zero exit status 1.\n",
      "inputparameters_2.cpp\n",
      "Error occurred while formatting inputparameters_2.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'inputparameters_2.cpp']' returned non-zero exit status 1.\n",
      "main.cpp\n",
      "Error occurred while formatting main.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'main.cpp']' returned non-zero exit status 1.\n",
      "mainwindow.cpp\n",
      "Error occurred while formatting mainwindow.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'mainwindow.cpp']' returned non-zero exit status 1.\n",
      "mainwindow.h\n",
      "Error occurred while formatting mainwindow.h: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'mainwindow.h']' returned non-zero exit status 1.\n",
      "mlog.cpp\n",
      "Error occurred while formatting mlog.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'mlog.cpp']' returned non-zero exit status 1.\n",
      "mythread.cpp\n",
      "Error occurred while formatting mythread.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'mythread.cpp']' returned non-zero exit status 1.\n",
      "paramcal.cpp\n",
      "Error occurred while formatting paramcal.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'paramcal.cpp']' returned non-zero exit status 1.\n",
      "paramcal_2.cpp\n",
      "Error occurred while formatting paramcal_2.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'paramcal_2.cpp']' returned non-zero exit status 1.\n",
      "queryparameters.cpp\n",
      "Error occurred while formatting queryparameters.cpp: Command '['c:\\\\Users\\\\Lenovo\\\\Desktop\\\\Softness\\\\my\\\\code\\\\clang-format.exe', '-i', 'queryparameters.cpp']' returned non-zero exit status 1.\n"
     ]
    }
   ],
   "source": [
    "import subprocess\n",
    "\n",
    "def format_cpp_file(file_path):\n",
    "    try:\n",
    "        # Run clang-format command to format the C++ file\n",
    "        subprocess.run([clang_format_path, '-i', file_path], check=True)\n",
    "        print(f\"{file_path} formatted successfully.\")\n",
    "    except subprocess.CalledProcessError as e:\n",
    "        print(f\"Error occurred while formatting {file_path}: {e}\")\n",
    "\n",
    "# Example usage:\n",
    "# 批量格式化\n",
    "for element in files:\n",
    "    print(element)\n",
    "    format_cpp_file(element)\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "清除注释"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 273,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceshi.cpp\n",
      "ceshi.cppformatted successfully\n",
      "ceshi.h\n",
      "ceshi.hformatted successfully\n",
      "inputparameters.cpp\n",
      "inputparameters.cppformatted successfully\n",
      "inputparameters_2.cpp\n",
      "inputparameters_2.cppformatted successfully\n",
      "main.cpp\n",
      "main.cppformatted successfully\n",
      "mainwindow.cpp\n",
      "mainwindow.cppformatted successfully\n",
      "mainwindow.h\n",
      "mainwindow.hformatted successfully\n",
      "mlog.cpp\n",
      "mlog.cppformatted successfully\n",
      "mythread.cpp\n",
      "mythread.cppformatted successfully\n",
      "paramcal.cpp\n",
      "paramcal.cppformatted successfully\n",
      "paramcal_2.cpp\n",
      "paramcal_2.cppformatted successfully\n",
      "queryparameters.cpp\n",
      "queryparameters.cppformatted successfully\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "\n",
    "\n",
    "def clearCcomment(file_path):\n",
    "    # 读取文件内容\n",
    "    with open(file_path, 'r') as file:\n",
    "        content = file.read()\n",
    "\n",
    "    # 使用正则表达式删除C++注释\n",
    "    # 删除多行注释 /* ... */\n",
    "    content = re.sub(r'/\\*.*?\\*/', '', content, flags=re.DOTALL)\n",
    "    # 删除单行注释 // ...\n",
    "    content = re.sub(r'//.*', '', content)\n",
    "\n",
    "    # 将修改后的内容写回文件\n",
    "    with open(file_path, 'w') as file:\n",
    "        file.write(content)\n",
    "for element in files: \n",
    "    print(element)\n",
    "    clearCcomment(element)\n",
    "    print(element+\"formatted successfully\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "清除空格"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 274,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceshi.cpp\n",
      "ceshi.cppformatted successfully\n",
      "ceshi.h\n",
      "ceshi.hformatted successfully\n",
      "inputparameters.cpp\n",
      "inputparameters.cppformatted successfully\n",
      "inputparameters_2.cpp\n",
      "inputparameters_2.cppformatted successfully\n",
      "main.cpp\n",
      "main.cppformatted successfully\n",
      "mainwindow.cpp\n",
      "mainwindow.cppformatted successfully\n",
      "mainwindow.h\n",
      "mainwindow.hformatted successfully\n",
      "mlog.cpp\n",
      "mlog.cppformatted successfully\n",
      "mythread.cpp\n",
      "mythread.cppformatted successfully\n",
      "paramcal.cpp\n",
      "paramcal.cppformatted successfully\n",
      "paramcal_2.cpp\n",
      "paramcal_2.cppformatted successfully\n",
      "queryparameters.cpp\n",
      "queryparameters.cppformatted successfully\n"
     ]
    }
   ],
   "source": [
    "def clearSpaces(file_path ):\n",
    "    # 以读写模式打开C++或H文件\n",
    "\n",
    "    # 读取文件内容\n",
    "    with open(file_path, 'r') as file:\n",
    "        lines = file.readlines()\n",
    "\n",
    "    # 删除空白行\n",
    "    non_empty_lines = [line for line in lines if line.strip()]\n",
    "\n",
    "    # 将修改后的内容写回文件\n",
    "    with open(file_path, 'w') as file:\n",
    "        file.write(''.join(non_empty_lines))\n",
    "\n",
    "for element in files:\n",
    "    print(element)\n",
    "    clearSpaces(element)\n",
    "    print(element+\"formatted successfully\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "获取代码行数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 275,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ceshi.cpp\n",
      "ceshi.cppformatted successfully\n",
      "文件 'ceshi.cpp' 共有 5581 行。\n",
      "ceshi.h\n",
      "ceshi.hformatted successfully\n",
      "文件 'ceshi.h' 共有 348 行。\n",
      "inputparameters.cpp\n",
      "inputparameters.cppformatted successfully\n",
      "文件 'inputparameters.cpp' 共有 28 行。\n",
      "inputparameters_2.cpp\n",
      "inputparameters_2.cppformatted successfully\n",
      "文件 'inputparameters_2.cpp' 共有 35 行。\n",
      "main.cpp\n",
      "main.cppformatted successfully\n",
      "文件 'main.cpp' 共有 76 行。\n",
      "mainwindow.cpp\n",
      "mainwindow.cppformatted successfully\n",
      "文件 'mainwindow.cpp' 共有 5582 行。\n",
      "mainwindow.h\n",
      "mainwindow.hformatted successfully\n",
      "文件 'mainwindow.h' 共有 348 行。\n",
      "mlog.cpp\n",
      "mlog.cppformatted successfully\n",
      "文件 'mlog.cpp' 共有 56 行。\n",
      "mythread.cpp\n",
      "mythread.cppformatted successfully\n",
      "文件 'mythread.cpp' 共有 1 行。\n",
      "paramcal.cpp\n",
      "paramcal.cppformatted successfully\n",
      "文件 'paramcal.cpp' 共有 763 行。\n",
      "paramcal_2.cpp\n",
      "paramcal_2.cppformatted successfully\n",
      "文件 'paramcal_2.cpp' 共有 839 行。\n",
      "queryparameters.cpp\n",
      "queryparameters.cppformatted successfully\n",
      "文件 'queryparameters.cpp' 共有 5 行。\n",
      "总行数 '13662' 行\n"
     ]
    }
   ],
   "source": [
    "def count_lines_in_cpp_file(file_path):\n",
    "    try:\n",
    "        # 打开C++文件并读取内容\n",
    "        with open(file_path, 'r') as file:\n",
    "            lines = file.readlines()\n",
    "        return len(lines)\n",
    "    except FileNotFoundError:\n",
    "        print(f\"文件 '{file_path}' 未找到。\")\n",
    "        return -1\n",
    "sum = 0\n",
    "for element in files:\n",
    "    print(element)\n",
    "    line_count = count_lines_in_cpp_file(element)\n",
    "    sum = sum + line_count\n",
    "    print(element+\"formatted successfully\")\n",
    "    if line_count >= 0:\n",
    "        print(f\"文件 '{element}' 共有 {line_count} 行。\")\n",
    "print(f\"总行数 '{sum}' 行\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生成docx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import shutil\n",
    "\n",
    "def copy_docx(original_docx_file_path, new_docx_file_path):\n",
    "    shutil.copyfile(original_docx_file_path, new_docx_file_path)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    original_docx_file_path = \"code_copy.docx\"\n",
    "    new_docx_file_path = \"code.docx\"\n",
    "\n",
    "    copy_docx(original_docx_file_path, new_docx_file_path)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "写入代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from docx import Document\n",
    "from docx.shared import Pt\n",
    "\n",
    "def read_txt_file(file_path):\n",
    "    with open(file_path, 'r', encoding='utf-8') as file:\n",
    "        return file.read().strip()  # 使用 strip() 方法去除首尾空白字符\n",
    "\n",
    "def add_text_to_docx(text, docx_file_path):\n",
    "    document = Document(docx_file_path)\n",
    "    paragraph = document.add_paragraph()\n",
    "    run = paragraph.add_run(text)\n",
    "    font = run.font\n",
    "    font.name = '宋体'\n",
    "    font.size = Pt(11)\n",
    "    document.save(docx_file_path)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    txt_file_path = \"code.txt\"\n",
    "    docx_file_path = \"code.docx\"\n",
    "\n",
    "    txt_content = read_txt_file(txt_file_path)\n",
    "    add_text_to_docx(txt_content, docx_file_path)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.15"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
